package com.mall.mallproduct.service.impl;

import com.mall.mallproduct.dao.AttrAttrgroupRelationDao;
import com.mall.mallproduct.entity.AttrAttrgroupRelationEntity;
import com.mall.mallproduct.entity.AttrEntity;
import com.mall.mallproduct.service.AttrAttrgroupRelationService;
import com.mall.mallproduct.service.AttrService;
import com.mall.mallproduct.vo.AttrGroupRelationVO;
import com.mall.mallproduct.vo.AttrGroupWithAttrsVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.utils.PageUtils;
import com.common.utils.Query;

import com.mall.mallproduct.dao.AttrGroupDao;
import com.mall.mallproduct.entity.AttrGroupEntity;
import com.mall.mallproduct.service.AttrGroupService;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {


    @Resource
    AttrAttrgroupRelationService relationService;
    @Resource
    AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");

        QueryWrapper<AttrGroupEntity>  wrapper=new QueryWrapper<AttrGroupEntity>();

        if (!StringUtils.isEmpty(key)){
            wrapper.and(obj->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        if (catelogId==0){
            IPage<AttrGroupEntity> page= this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return  new PageUtils(page);
        }else {

            wrapper.eq("catelog_id",catelogId);
            IPage<AttrGroupEntity> page= this.page(new Query<AttrGroupEntity>().getPage(params),
                   wrapper);
            return  new PageUtils(page);
        }

    }

    @Override
    public void saveRelation(List<AttrGroupRelationVO> relationVO) {

        List<AttrAttrgroupRelationEntity> collect = relationVO.stream().map(item -> {
            AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, entity);
            return entity;
        }).collect(Collectors.toList());
        relationService.saveBatch(collect);
    }

    @Override
    public List<AttrGroupWithAttrsVO> getAttrGroupWithAttrsBycatelogId(Long catelogId) {

        List<AttrGroupEntity> attrGroupEntities = this.list(
                new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        List<AttrGroupWithAttrsVO> collect = attrGroupEntities.stream().map(item -> {
            AttrGroupWithAttrsVO vo = new AttrGroupWithAttrsVO();
            BeanUtils.copyProperties(item, vo);
            List<AttrEntity> attrs = attrService.getAttrRelation(item.getAttrGroupId());
            vo.setAttrs(attrs);
            return vo;
        }).collect(Collectors.toList());

        return  collect;
    }

}